package com.heaven.service.impl;

import com.heaven.core.dao.DanmuDao;
import com.heaven.core.entity.DanmuEntity;
import com.heaven.core.framework.PzServiceImpl;
import com.heaven.service.DanmuService;
import com.heaven.customerServer.customerController.vo.danmu.SendDanmuReq;
import com.heaven.service.VideoService;
import com.heaven.utils.BeanUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service("danmuService")
@Slf4j
public class DanmuServiceImpl extends PzServiceImpl<DanmuDao, DanmuEntity> implements DanmuService {
    @Autowired
    private VideoService videoService;

    @Override
    public boolean saveDanmu(SendDanmuReq req, String userId) {
        // 检验videoId是否合法
        if(videoService.selectById(req.getVideoId()) == null) {
            return false;
        }
        DanmuEntity entity = new DanmuEntity();
        BeanUtils.copyProperties(req, entity);
        entity.setUserId(userId);
        return this.save(entity);
    }

//    @Override
//    @Cacheable(value = "danmuPool", key = "#root.args[0]")
//    public List<List<Object>> convertToDplayerDanmuPool(String videoId) {
//        Query query = new Query();
//        query.addCriteria(Criteria.where("videoId").is(videoId));
//        List<DanmuEntity> danmuPool = selectList(query);
//
//        // 转换
//        List<List<Object>> ans = new ArrayList<>();
//        for (DanmuEntity danmu : danmuPool) {
//            List<Object> tmp = new ArrayList<>();
//            // 严格按照顺序添加，否则Dplayer无法识别
//            tmp.add(danmu.getTime());
//            //tmp.add(danmu.getType());
//            tmp.add(danmu.getColor());
//            //tmp.add(danmu.getAuthor());
//            tmp.add(danmu.getText());
//            ans.add(tmp);
//        }
//        return ans;
//    }

    @Override
    @Cacheable(value = "danmuPool", key = "#root.args[0]")
    public List<DanmuEntity> loadArtPlayerDanmuPool(String videoId) {
        Query query = new Query();
        query.addCriteria(Criteria.where("videoId").is(videoId));
        return selectList(query);
    }
}
